<?php

namespace App\Http\Requests\Logic\Template;

use Illuminate\Validation\Rule;
use Illuminate\Foundation\Http\FormRequest;

/**
 * 创建规则模板请求逻辑
 *
 * Date: 2018/6/11
 * @author George
 * @package App\Http\Requests\Logic\Template
 */
class StoreResource extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

	/**
	 * 定义表单验证规则
	 *
	 * Date: 2018/6/11
	 * @author George
	 * @return array
	 */
    public function rules()
    {
        return [
        	'pattern_id' => 'required',
        	'name' => 'required',
        	'type' => [
        		'required',
				Rule::in(array_keys(config('finance.template.type')))
			],
			'ignore' => 'required_if:type,advanced',
			'conditions' => 'required|array',
			'conditions.*.id' => 'nullable|numeric',
			'conditions.*.type' => [
				'required_if:type,advanced',
				Rule::in(array_keys(config('finance.regulation.type')))
			],
        	'conditions.*.operator_one' => 'required',
        	'conditions.*.reference_one' => 'required|numeric',
        	'conditions.*.operator_two' => 'required_if:type,staircase',
        	'conditions.*.reference_two' => 'required_if:type,staircase|numeric',
        	'conditions.*.value' => 'required|numeric',
        	'conditions.*.expression' => 'nullable',
        	'conditions.*.literals' => 'nullable'
        ];
    }

	/**
	 * 定义表单验证错误提示信息
	 *
	 * Date: 2018/6/11
	 * @author George
	 * @return array
	 */
	public function messages()
	{
		return [
			'pattern_id.required' => '方案ID有误',
			'name.required' => '请输入模板名称',
			'type.required' => '请选择类型',
			'type.in' => '模板类型有误',
			'conditions.*.type.required_if' => '请选择条件类型',
			'conditions.*.type.in' => '条件公式的类型有误',
			'conditions.*.operator_one.required_if' => '条件操作符有误',
			'conditions.*.reference_one.required' => '请填写参考条件的值',
			'conditions.*.reference_one.numeric' => '值必须为数字',
			'conditions.*.reference_two.required_if' => '请填写第二参考条件值',
			'conditions.*.reference_two.numeric' => '值必须为数字',
			'conditions.*.value.required' => '请填写值',
			'conditions.*.value.numeric' => '值必须为数字'
		];
    }
}
